<?php
defined('IN_LM') or die('Error -1'); //防止非法调用

//这个文件应该改名成 user

function chack_login_data()
{
    if(
          $_POST['acc']==""
        ||$_POST['pass']==""
        //||match_xx($_POST['acc'])
        )
    {
        return true;
    }else
    {
        return false;
    }
}

function chack_login_pass_at_id($id,$pass)
{
    $user=$GLOBALS['sql']->read_data_on_tab(
        "users",
        array(
            'id'=>$id,
            'pass'=>$pass,
        ));
    return $user;
}

function read_user_at_acc($acc)
{
    $user=$GLOBALS['sql']->read_data_on_tab(
        "users",
        array(
            'acc'=>$acc,
        ));
    return $user;
}

function read_user_at_id($id)
{
    $user=$GLOBALS['sql']->read_data_on_tab(
        "users",
        array(
            'id'=>$id,
        ));
    return $user;
}

function chack_token_at_user_id($key,$userid)
{
    $token=$GLOBALS['sql']->read_data_on_tab(
        "tokens",
        array(
            'token_key'=>$key,
            'user_id'=>$userid,
        ));

    if($token==null)
    {
        return false;
    }else
    {
        return true;
    }
   
}

function login_user($acc,$pass)
{
    $GLOBALS['sql']->addend_data_on_tab(
        "users",
        array(
            'acc'=>$acc,
            'pass'=>$pass,
            'date'=>date('Y-m-d H:i:s'),
            'lastlogin'=>date('Y-m-d H:i:s'),
        ));
}

function login(&$return_data)
{
    $return_data['ac']="login";
    switch($_GET['do'])
    {
        case "login":
            if(chack_login_data())
            {
                $return_data['error_msg']="data_error";
                $return_data['error_code']=100;
                return;
            }
            $_POST['pass']=md5($_POST['pass']);
            $user=read_user_at_acc($_POST['acc']);

            if($user==NULL)
            {
                //数据库没有,写入数据库
               
                login_user($_POST['acc'],$_POST['pass']);
                
                $user=read_user_at_acc($_POST['acc']);
                if($user==NULL)
                {
                    $return_data['error_code']=101;
                    $return_data['error_msg']="内部错误101";
                }else
                {
                    $return_data['error_code']=0;
                    $return_data['error_msg']="成功注册，成功登录";
                    $return_data['user']=$user[0];
                }
                
            }else
            {
               
                if($user[0]['pass']==$_POST['pass'])//pass ok
                {
                    //up last login
                    $GLOBALS['sql']->updata_on_tab(
                        "users",
                        array(
                            'id'=>$user[0]['id']
                        ),
                        array(
                            'lastlogin'=>date('Y-m-d H:i:s')
                        )
                    );

                    $user=read_user_at_acc($_POST['acc']);
                    $return_data['user']=$user[0];
                    $return_data['error_code']=0;
                    $return_data['error_msg']="账号已注册，成功登录";
                }else
                {
                    $return_data['user']=array();
                    $return_data['error_code']=10;
                    $return_data['error_msg']="密码错误";
                    
                }
                
                
            }


            
            //$return_data['error_msg']="logined";
        break;
    }

}








